package handlers

import (
	"github.com/gin-gonic/gin"
	"pic-share-sys/models"
	"pic-share-sys/pkg"
)

func Register(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	tel := c.PostForm("tel")

	if !pkg.RegTel(tel) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "手机号必须是11位",
		})
		return
	}

	if !pkg.RegPass(password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	user := models.User{
		Username: username,
		Password: pkg.Md5(password),
		Tel:      tel,
	}
	err := models.DB.Create(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "注册失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "注册成功",
		})
		return
	}

}
